﻿#include "PLCCommOPCUA.h"

PLCCommOPCUA::PLCCommOPCUA(QObject *parent)
    : QObject{parent}
{

    m_client = new Open62541();

    m_pReadThread = new PLCReadThread(m_client, this);
    m_pWriteThread = new PLCWriteThread(m_client, this);

    connect(m_pReadThread, &PLCReadThread::finished, m_pReadThread, &QObject::deleteLater);
    connect(m_pWriteThread, &PLCWriteThread::finished, m_pWriteThread, &QObject::deleteLater);

}

PLCCommOPCUA::~PLCCommOPCUA()
{
    m_pReadThread->stop();
    m_pWriteThread->stop();
    //m_pReadThread->terminate();
    //m_pWriteThread->terminate();
}

void PLCCommOPCUA::init(const QString &endpointUrl)
{
    m_client->Open62541_Init(endpointUrl);
}

void PLCCommOPCUA::start()
{
    m_pReadThread->start();
    m_pWriteThread->start();
}

void PLCCommOPCUA::writeValveStatus(int valveChannel, bool status)
{
    m_pWriteThread->writeValveStatus(valveChannel, status);
}

void PLCCommOPCUA::writeAllValveStatus(const QVector<bool> &status)
{
    m_pWriteThread->writeAllValveStatus(status);
}

void PLCCommOPCUA::writeFlowRate(int flowmeterChannel, int16_t inputValue)
{
    m_pWriteThread->writeFlowRate(flowmeterChannel, inputValue);
}

void PLCCommOPCUA::writeAllFlowRate(const QVector<int16_t> &inputValue)
{
    m_pWriteThread->writeAllFlowRate(inputValue);
}

void PLCCommOPCUA::writePressure(int pressuremeterChannel, int16_t inputValue)
{
    m_pWriteThread->writePressure(pressuremeterChannel, inputValue);
}

void PLCCommOPCUA::writeAllPressure(const QVector<int16_t> &inputValue)
{
    m_pWriteThread->writeAllPressure(inputValue);
}

void PLCCommOPCUA::writeMKS(int16_t pressure, int16_t openingAmplitude, int16_t switchMode)
{
    m_pWriteThread->writeMKS(pressure, openingAmplitude, switchMode);
}

void PLCCommOPCUA::writeTemperature(bool status1, bool status2, int16_t temperature1, int16_t temperature2)
{
    m_pWriteThread->writeTemperature(status1, status2, temperature1, temperature2);
}


// 下面都是读取相关的函数
bool PLCCommOPCUA::readValveStatus(int valveChannel)
{
    QMutexLocker locker(&m_mutex);
    bool value = m_valveStatus[valveChannel];
    return value;
}

double PLCCommOPCUA::readFlowRate(int flowmeterChannel)
{
    QMutexLocker locker(&m_mutex);
    double value = m_flowMeter[flowmeterChannel];
    return value;
}

double PLCCommOPCUA::readPressure(int pressuremeterChannel)
{
    QMutexLocker locker(&m_mutex);
    double value = m_pressureMeter[pressuremeterChannel]; // 拷贝一份，防止 PLCReadThread 正在修改这个结果。
    return value;
}

void PLCCommOPCUA::readMKS(double &pressure, int16_t &openingAmplitude)
{
    QMutexLocker locker(&m_mutex);
    pressure = m_mks_pressure;
    openingAmplitude = m_mks_openingAmplitude;
}

void PLCCommOPCUA::readTemperature(double &temperature1, double &temperature2)
{
    QMutexLocker locker(&m_mutex);
    temperature1 = m_temperature[0];
    temperature2 = m_temperature[1];
}

bool PLCCommOPCUA::readOtherValveStatus(int valveChannel)
{
    QMutexLocker locker(&m_mutex);
    bool value = m_otherValveStatus[valveChannel];
    return value;
}
